/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package almacen;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import resources.local_connection;

/**
 *
 * @author pato
 */
public class RetiroEspecial extends javax.swing.JFrame {

    /**
     * Creates new form RetiroMaterial
     */
    String nombre;
    float cantidad;//esta es la cantidad original del producto
    int codigo;
    PreparedStatement ps;
    ResultSet rs;
    private final DefaultListModel modelo1;
    int nSuc;//esta variable se refiere a si es la sucursal uno o dos del almacen
    //esto es para jalar tanto el nombre como el id de forma estatica
    //de la clase principal
    public RetiroEspecial(int id, String nom, int nuc,float c) {
        initComponents();
        modelo1 = new DefaultListModel();
        listaSuc1.setModel(modelo1);
        nombre = nom;
        codigo=id;
        nSuc=nuc;
        cantidad=c;
        cargarDatos();
    }
private void cargarDatos(){
        try {
            String nombreSuc;
            if(nSuc==1){
                nSuc=Almacen.suc1;
                nombreSuc=Almacen.nSuc1;
            }
            else{
                nSuc=Almacen.suc2;
                nombreSuc=Almacen.nSuc2;
            }
      //      System.out.println(nSuc);
            lblNombre.setText(nombre);
            lblId.setText(String.valueOf(codigo));
            lblSuc.setText(nombreSuc);
            ps = local_connection.getconnect().prepareStatement("select clave, sucursal from especiales where vendido=0 and id_producto=? and sucursal = ? ");
            ps.setInt(1, codigo);
            ps.setInt(2, nSuc);
            rs = ps.executeQuery();
         //   System.out.println(ps);
            while (rs.next()) {
                modelo1.addElement(rs.getString("clave"));

            }
        } catch (SQLException ex) {
Almacen.logger.log(Level.SEVERE, null, ex);        }
}
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblId = new javax.swing.JLabel();
        lblNombre = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lblSuc = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        motivo = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listaSuc1 = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lblId.setText("----");

        lblNombre.setText("----");

        jLabel1.setText("Sucursal:");

        lblSuc.setText("----");

        jLabel3.setText("Motivo");

        motivo.setColumns(20);
        motivo.setRows(5);
        jScrollPane1.setViewportView(motivo);

        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel4.setText("Clave");

        jScrollPane2.setViewportView(listaSuc1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblId)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblNombre))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblSuc)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel3)))))
                .addGap(74, 74, 74))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblId)
                    .addComponent(lblNombre))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblSuc))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
if (listaSuc1.isSelectionEmpty()) {
    JOptionPane.showMessageDialog(this, "Tienes que seleccionar al menos una clave", "Error", JOptionPane.ERROR_MESSAGE);
}   else{ 
        try {
             ps=local_connection.getconnect().prepareStatement("insert into retiro_especial(clave,motivo,id_usuario) values(?,?,?)");
                         String clave;
            List<String> claves = listaSuc1.getSelectedValuesList();
            for (int i = 0; i < claves.size(); i++) {
                clave = claves.get(i);
                modelo1.removeElement(clave);
                ps.setString(1,clave);
             ps.setString(2,motivo.getText());
             ps.setInt(3, Almacen.idUsuario);
             ps.execute();
            }
             
             this.dispose();
        } catch (SQLException ex) {
Almacen.logger.log(Level.SEVERE, null, ex);        }
}
        
// TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
this.dispose();
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
  */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblId;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JLabel lblSuc;
    private javax.swing.JList listaSuc1;
    private javax.swing.JTextArea motivo;
    // End of variables declaration//GEN-END:variables
}
